/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.client.command.impl;

import org.soupframework.gwt.designerpanel.client.designer.ContainerDesigner;
import org.soupframework.gwt.designerpanel.client.designer.Designer;
import org.soupframework.gwt.designerpanel.client.util.Util;

public class ContainerMoveFromContainerCommand<T> extends ContainerDesignerCommandBase<T> {

	private ContainerDesigner<T> sourceDesigner;
	private T object;
	private int left;
	private int top;
	private Integer oldLeft;
	private Integer oldTop;

	ContainerMoveFromContainerCommand(ContainerDesigner<T> designer, ContainerDesigner<T> sourceDesigner, T object, int left, int top) {
		super(designer);
		this.sourceDesigner = sourceDesigner;
		this.object = object;
		this.left = left;
		this.top = top;
		this.oldLeft = Util.getWidgetStyleLeft(sourceDesigner.getChildObjectView(object));
		this.oldTop = Util.getWidgetStyleTop(sourceDesigner.getChildObjectView(object));
	}

	@Override
	protected void doCommandInt() {
		Designer<T> d = this.sourceDesigner.getChildDesigner(object);
		this.sourceDesigner.getOperations().removeObject(object);
		this.getDesigner().getOperations().addObject(this.object, this.left, this.top);
		if(d != null){
			this.sourceDesigner.removeChildDesigner(d);
			this.getDesigner().addChildDesigner(d);
		}
	}

	@Override
	protected void undoCommandInt() {
		Designer<T> d = this.getDesigner().getChildDesigner(object);
		this.getDesigner().getOperations().removeObject(object);
		this.sourceDesigner.getOperations().addObject(this.object, this.oldLeft != null ? this.oldLeft : 0, this.oldTop != null ? this.oldTop : 0);
		if(d != null){
			this.getDesigner().removeChildDesigner(d);
			this.sourceDesigner.addChildDesigner(d);
		}
	}
}
